//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Query Application Block
//===============================================================================

using System;
using System.Configuration;
using System.Linq.Expressions;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ContainerModel;
using EntLibContrib.Query.Queries;

namespace EntLibContrib.Query.Configuration
{
    using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Design;

    /// <summary>
	/// Configuration data defining WcfServiceQueryData.
	/// </summary>    
    [ResourceDescription(typeof(DesignResources), "WcfServiceQueryDataDescription")]
    [ResourceDisplayName(typeof(DesignResources), "WcfServiceQueryDataDisplayName")]
	public class WcfServiceQueryData : ServiceQueryData
	{
		private const string endpointProperty = "endpoint";
		
        /// <summary>
		/// Initializes a new instance of the <see cref="WcfServiceQueryData"/> class.
		/// </summary>
		public WcfServiceQueryData () 
            : base(null, typeof(WcfServiceQuery))
		{
		}

		/// <summary>
		/// Initialize a new instance of the <see cref="WcfServiceQueryData"/> class.
		/// </summary>
		/// <param name="name">The name of the <see cref="WcfServiceQueryData"/>.</param>
		/// <param name="customCommandName">Name of the custom command.</param>
		/// <param name="parameterSetName">Name of the parameter set.</param>
		/// <param name="address">The service address.</param>
		/// <param name="service">The service type name.</param>
		/// <param name="method">The service method.</param>
		/// <param name="endpoint">The service end point.</param>
		public WcfServiceQueryData(string name, string customCommandName, string parameterSetName, string address, string service, string method, string endpoint)
			: base(name, typeof(WcfServiceQuery), customCommandName, parameterSetName, address, service, method)
		{
			this.Endpoint = endpoint;
		}

        /// <summary>
		/// Name of the end point to use.
		/// </summary>
		/// <value>The name of the end point.</value>
		[ConfigurationProperty(endpointProperty, IsRequired = true)]
        [ResourceDescription(typeof(DesignResources), "WcfServiceQueryDataEndpointDescription")]
        [ResourceDisplayName(typeof(DesignResources), "WcfServiceQueryDataEndpointDisplayName")]
		public string Endpoint
		{
			get { return (string)base[endpointProperty]; }
			set { base[endpointProperty] = value; }
		}

        /// <summary>
        /// Returns the <see cref="TypeRegistration"/> entry for the current concrete type represented by this 
        /// configuration object.
        /// </summary>
        /// <returns>A registry entry.</returns>
		protected override Expression<Func<IQuery>> GetQueryCreationExpression()
		{
			return () =>
				new WcfServiceQuery(
			       	Name,
			       	Endpoint,
			       	Address,
			       	Service,
			       	Method,
                    Container.ResolvedIfNotNull<ICommand>(CustomCommandName),
                    Container.ResolvedIfNotNull<IParameterSet>(ParameterSetName));
		}
	}
}